package com.yjd.merchant.app.api.org;

import com.yjd.comm.base.control.MerchantBaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.merchant.dto.MerchantLimitSetDto;
import com.yjd.comm.merchant.model.MerchantFundsModel;
import com.yjd.comm.merchant.service.IMerchantFundsService;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.vo.Constant;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商户端APP 机构相关操作
 * Created by yangchangyan on 2017/11/28.
 */
@Controller("com.yjd.merchant.app.api.org.OrganizationControl")
@Scope("prototype")
@RequestMapping("/merchant/org")
public class OrganizationControl extends MerchantBaseControl {

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private IMerchantFundsService merchantFundsService;

    /**
     * 店铺信息
     *
     * @return
     * @throws
     */
    @RequestMapping(value = "/info")
    public void orgInfo() throws Exception{
        Long org_id = super.getMerchantSession().getOrg_id();
        OrganizationModel org = organizationService.getModelById(org_id, DbWREnums.READ);
        if(null == org){
            this.errorMsg("机构不存在");
        }

        //机构分期期数
        List<Integer> periodsList = null;
        if(StringUtil.isEmpty(org.getLimit_config())){
            String defaultPeriod = SysParamUtil.getValue(Constant.SysParamKeyEnum.MFQ_PERIODS_CONFIG);
            periodsList = StringUtil.getObject(defaultPeriod ,List.class);
        } else {
            periodsList = StringUtil.getObject(org.getLimit_config() ,List.class);
        }

        Double available = 0.0;
        MerchantFundsModel funds = merchantFundsService.getModelById(org_id, DbWREnums.READ);
        if(null != funds){
            available = funds.getAvailable();
        }

        Map result = FrameUtil.newHashMap();
        result.put("org_name", org.getOrg_name());
        result.put("org_pic", ServletUtil.getFileUrl(org.getOrg_pic()));
        result.put("periodsList", periodsList);
        result.put("available", available);
        this.successData(result);
    }

    /**
     * 修改机构分期数
     *
     * @return
     * @throws
     */
    @RequestMapping(value = "/set/limit")
    public void setLimit(MerchantLimitSetDto limitSetDto) throws Exception{
        if (limitSetDto == null || limitSetDto.getLimitList() == null) {
            FrameUtil.throwBaseException1("请求参数不能为空");
        }
        organizationService.updateOrgLimit(super.getMerchantId(), limitSetDto.getLimitList());
        this.successMsg("项目期限配置成功");
    }

    /**
     * 系统默认分期数
     *
     * @return
     * @throws
     */
    @RequestMapping(value = "/default/limit")
    public void defaultLimit() throws Exception{
        String defaultPeriod = SysParamUtil.getValue(Constant.SysParamKeyEnum.MFQ_PERIODS_CONFIG);
        List<Integer> periodsList = StringUtil.getObject(defaultPeriod, List.class);
        this.successData(FrameUtil.newHashMap("periodsList", periodsList));
    }
}
